/*
 * AccountServiceClient.java
 * 
 * Client that opens the connection to the AccountService.
 * 
 * Created: 14.11.2008
 * 
 * Copyright (C) 2008 Fraunhofer SCAI
 * 
 * All rights reserved
 * 
 * https://gforge.beingrid.eu/gf/project/scailm/
 * 
 */
package de.fraunhofer.scai.lm.accountservice.client;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.GetAccountInfo;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.GetAccountInfoResponse;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.GetLicenseAccounts;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.GetLicenseAccountsResponse;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.CreateLicenseAccount;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.CreateLicenseAccountResponse;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.DeleteLicenseAccount;
import de.fraunhofer.scai.lm.accountservice.client.gen.AccountServiceStub.DeleteLicenseAccountResponse;
import de.fraunhofer.scai.lm.gui.Account;

/**
 * Implementation of the AccountServiceClient.
 * 
 * @author Yona Raekow
 */
public class AccountServiceClient {

    /**
     * The service stub packs the elements that are sent over the network.
     */
    private AccountServiceStub serviceStub;

    private String email = "";
    private String organization = "";
    private String responsible = "";
    
	/**
     * Logger for the AccountServiceClient class.
     */
    static Logger logger = Logger.getLogger(AccountServiceClient.class);

    /**
     * Constructor initializes the service stub
     * 
     * @param serviceStub
     *                Interface to the account web service
     */
    public AccountServiceClient(AccountServiceStub serviceStub) {
    	this.serviceStub = serviceStub;
    }

    /**
     * This method has as input a String that is being submitted to the web
     * service that handles the account management. A String consisting of all
     * licenseAccountNames belonging to this user is returned. The license
     * accounts are comma separated.
     * 
     * @param String
     *                userId of the superuser that is allowed to manage a set of
     *                license accounts
     * @throws RemoteException
     *                 in case there is a problem with the connection between
     *                 client and server
     */
    public String getLicenseAccounts(String billingAccount, String password) throws RemoteException, Exception {
		/*
		 * setting up the input for the web service operation
		 */
		GetLicenseAccounts request = new GetLicenseAccounts();
		request.setBillingAccount(billingAccount);
	
		request.setPassword(password);
		
		logger.debug("AccountHolder is: " + request.getBillingAccount());
		logger.debug("Password is: " + request.getPassword());
		
		// retrieving the response
		GetLicenseAccountsResponse response = serviceStub.getLicenseAccounts(request);
		String licenseAccountList = "";
		try{
			licenseAccountList = response.getLicenseAccounts();
			organization = response.getOrganization();
			email = response.getEmail();
			responsible = response.getResponsible();
		}catch(Exception e)
		{
			licenseAccountList = response.getLicenseAccounts();
			if(licenseAccountList.equals("EmptyList"))
				throw new Exception("Cannot get license accounts");
			e.printStackTrace();
		}
		return licenseAccountList;

    }

  /*  *//**
     * Invokes the web service that should create a new license account.
     * Parameters are the account name and the organization to which this
     * account belongs.
     * 
     * @param newAccountName
     *                the name of the new license account
     * @return whether or not the account was successfully created
     * @throws RemoteException
     *                 in case there is a problem with the network connection
     */
    public boolean createLicenseAccount(String billingAccount, String password, String licenseAccountName, String accountType) 
    throws RemoteException {
		
		 /* setting up the input for the web service operation*/
		 System.out.println("createLicenseAccount");
		CreateLicenseAccount request = new CreateLicenseAccount();
		request.setLicenseAccountName(licenseAccountName);
		request.setBillingAccount(billingAccount);
		request.setPassword(password);
		request.setAccountType(accountType);

		// retrieving the response
		CreateLicenseAccountResponse response = serviceStub
		.createLicenseAccount(request);
		logger.debug("newAccountName is: " + request.getLicenseAccountName());
		logger.debug("billingAccount is: " + request.getBillingAccount());
		return response.getSuccess();

    }

    /**
     * Invokes the web service that deletes the license account with the
     * provided name.
     * 
     * @param accountName
     *                the name of the license account to be deleted
     * @return whether the operation was successful
     * @throws RemoteException
     *                 in case there is a problem with the network connection
     */
    public boolean deleteLicenseAccount(String billingAccount, String password, String licenseAccountName) 
    throws RemoteException {
    	logger.debug("account: " + licenseAccountName + " is to be deleted");
		
		 /* setting up the input for the web service operation */
    	DeleteLicenseAccount request = new DeleteLicenseAccount();
		request.setLicenseAccountName(licenseAccountName);
		request.setBillingAccount(billingAccount);
		request.setPassword(password);

	
		// retrieving the response
		DeleteLicenseAccountResponse response = serviceStub
		.deleteLicenseAccount(request);
		return response.getSuccess();

    }

    /**
     * This method invokes the web service that returns information of a certain
     * license account.
     * 
     * @param accountName
     *                the name of the license account
     * @param responsible
     *                the name of the superuser responsible for the license
     *                account
     * @return an Account with all the necessary Information
     * @throws RemoteException
     *                 in case there is a problem with the network connection
     **/

    public Account getAccountInfo(String billingAccount, String password, String licenseAccountName)
    throws RemoteException {
		//setting up the input for the web service operation 
		 
		GetAccountInfo accountInfo = new GetAccountInfo();
		accountInfo.setLicenseAccountName(licenseAccountName);
		accountInfo.setBillingAccount(billingAccount);
		accountInfo.setPassword(password);
		// retrieving the response
		GetAccountInfoResponse response = serviceStub
		.getAccountInfo(accountInfo);
	
		
		//Processing the response 
		 
		List<String> featureUsageCostsList = new ArrayList<String>();
		String detailString = response.getFeatureUsageCosts();
		featureUsageCostsList = Arrays.asList(detailString.split(";"));
		logger.debug("response: " + response.getFeatureUsageCosts());
		logger.debug("responsible is: " + responsible);

		 //Create a new Account object locally with the information
		 
/*		Account accountInfoResponse = new Account(licenseAccountName,
			(Integer) response.getTanListNumber(), (Integer) response
			.getNumberOfTans(), (Integer) response.getUnusedTans(),
			(Integer) response.getCPUHours(), (Boolean) response
			.getIsBlocked(), newAccountDetails);*/
		Account accountInfoResponse = new Account(licenseAccountName,
				(Integer) response.getTanListNumber(),(Integer) response
				.getNumberOfTans(), (Integer) response.getUnusedTans(),
				(Integer) 12, (Boolean) response
				.getIsBlocked(), featureUsageCostsList);
		return accountInfoResponse;

    }
    


	public String getEmail() {
		return email;
	}

	private void setEmail(String email) {
		this.email = email;
	}

	public String getOrganization() {
		return organization;
	}

	private void setOrganization(String organization) {
		this.organization = organization;
	}

	public String getResponsible() {
		return responsible;
	}
}
